
#include <stdarg.h>
#include <assert.h>
#include "wormnet/ILogs.h"
#include "wmLogs.h"

namespace worm
{

wmLogs g_oLogs;

WORM_DLL_EXPORT void InitLogs(const uint8& byMinLogLvl)
{
    g_oLogs.Initialize(byMinLogLvl);
}

#define MAX_LOG_SIZE    (4*1024)

WORM_DLL_EXPORT void PrintInfo(const char* szLog, ...)
{
    if (!g_oLogs.IsPrint(google::INFO))
    {
        return;
    }

    SLog* pLog = new(SLog);
    pLog->m_byLogLvl = google::INFO;
    pLog->m_szLog = new char[MAX_LOG_SIZE];

    va_list ap;
    va_start(ap, szLog);
    vsnprintf(pLog->m_szLog, MAX_LOG_SIZE, szLog, ap);
    va_end(ap);

    g_oLogs.AddLog(pLog);
}

WORM_DLL_EXPORT void PrintWarning(const char* szLog, ...)
{
    SLog* pLog = new SLog;
    pLog->m_byLogLvl = google::WARNING;
    pLog->m_szLog = new char[MAX_LOG_SIZE];

    va_list ap;
    va_start(ap, szLog);
    vsnprintf(pLog->m_szLog, MAX_LOG_SIZE, szLog, ap);
    va_end(ap);

    g_oLogs.AddLog(pLog);
}

WORM_DLL_EXPORT void PrintError(const char* szLog, ...)
{
    SLog* pLog = new SLog;
    pLog->m_byLogLvl = google::ERROR;
    pLog->m_szLog = new char[MAX_LOG_SIZE];

    va_list ap;
    va_start(ap, szLog);
    vsnprintf(pLog->m_szLog, MAX_LOG_SIZE, szLog, ap);
    va_end(ap);

    g_oLogs.AddLog(pLog);
}

WORM_DLL_EXPORT void PrintFatal(const char* szLog, ...)
{
    SLog* pLog = new SLog;
    pLog->m_byLogLvl = google::FATAL;
    pLog->m_szLog = new char[MAX_LOG_SIZE];

    va_list ap;
    va_start(ap, szLog);
    vsnprintf(pLog->m_szLog, MAX_LOG_SIZE, szLog, ap);
    va_end(ap);

    g_oLogs.AddLog(pLog);
}

WORM_DLL_EXPORT void ReleaseLogs()
{
    g_oLogs.Release();
}

WORM_DLL_EXPORT void SetLogDest(const int& nLvl, const char* pFileNmaePre)
{
    assert(NULL != pFileNmaePre);
    assert(nLvl < google::NUM_SEVERITIES);

    google::SetLogDestination(nLvl, pFileNmaePre);
}

}
